Ext.define('AM.controller.UserCreate', {
    extend: 'Ext.app.Controller',

    stores: [
        'Users@AM.store'
    ],

    models: [
        'User@AM.model'
    ],

    views: [
        'UserCreateWindow@AM.view.tasks'
    ],

    refs: [
        {
            ref: 'usersPanel',
            selector: 'panel'
        }
    ],

    init: function() {
        this.control({
            'viewport > userlist': {
                itemdblclick: this.editUser
            },
            'userCreateWindow button[action=save]': {
                click: this.updateUser
            }
        });
    },

    editUser: function(grid, record) {
        var edit = Ext.create('AM.view.user.Edit').show();

        edit.down('form').loadRecord(record);
    },

    updateUser: function(button) {
        var win    = button.up('window'),
            form   = win.down('form'),
            record   = Ext.create('AM.model.User');
            values = form.getValues();

        record.set(values);
        win.close();
        
        /*record.save({
			success: function(rec, op) {
				Ext.Msg.alert('Saved');
			},
			failure: function(rec, op) {
			    Ext.Msg.alert("Failed",op.request.scope.reader.jsonData["message"]);
			}
		});*/

        var userStore = this.getUsersStore();
        userStore.sync();
    }
});
